package com.pro01.backend.controller;

import com.pro01.backend.pojo.Result;
import com.pro01.backend.service.ForumCommentService;
import com.pro01.backend.utils.TokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/posts")
public class AdminCommentController {

    @Autowired
    private ForumCommentService forumCommentService;

    /**
     * 管理员删除任意评论
     */
    @DeleteMapping("/{postId}/comments/{commentId}")
    public Result<Void> deleteCommentByAdmin(@PathVariable Long postId,
                                             @PathVariable Long commentId,
                                             HttpServletRequest request) {
        // 校验管理员身份
        String role = TokenUtil.getRole(request);
        if (!"ADMIN".equals(role)) {
            return Result.error("无权限，只有管理员可用！");
        }

        try {
            forumCommentService.deleteCommentByAdmin(commentId);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("管理员删除评论失败：" + e.getMessage());
        }
    }

    /**
     * 管理员隐藏评论
     */
    @PutMapping("/{postId}/comments/{commentId}/hide")
    public Result<Void> hideCommentByAdmin(@PathVariable Long postId,
                                           @PathVariable Long commentId,
                                           HttpServletRequest request) {
        // 校验管理员身份
        String role = TokenUtil.getRole(request);
        if (!"ADMIN".equals(role)) {
            return Result.error("无权限，只有管理员可用！");
        }

        try {
            forumCommentService.hideCommentByAdmin(commentId);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("隐藏评论失败：" + e.getMessage());
        }
    }

    /**
     * 管理员恢复评论
     */
    @PutMapping("/{postId}/comments/{commentId}/restore")
    public Result<Void> restoreCommentByAdmin(@PathVariable Long postId,
                                              @PathVariable Long commentId,
                                              HttpServletRequest request) {
        // 校验管理员身份
        String role = TokenUtil.getRole(request);
        if (!"ADMIN".equals(role)) {
            return Result.error("无权限，只有管理员可用！");
        }

        try {
            forumCommentService.restoreCommentByAdmin(commentId);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("恢复评论失败：" + e.getMessage());
        }
    }
}
